package client;

import dao.CustomerDAO;
import dao.ProductDAO;
import dao.BossDAO;
import model.Customer;
import model.Product;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 顾客客户端
 */
public class CustomerClient {
    public static void main(String[] args) {
        try (BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
            
            System.out.println("欢迎使用超市管理系统-顾客端");
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.print("请选择: ");
            
            String choice = stdIn.readLine();
            CustomerDAO customerDAO = new CustomerDAO();
            String customerId = null;
            
            if ("1".equals(choice)) {
                // 登录
                System.out.print("请输入顾客ID: ");
                customerId = stdIn.readLine();
                System.out.print("请输入密码: ");
                String password = stdIn.readLine();
                
                Customer customer = customerDAO.login(customerId, password);
                if (customer == null) {
                    System.out.println("登录失败，ID或密码错误");
                    return;
                }
                System.out.println("登录成功！");
                showCustomerMenu(stdIn, customerId, customer.isMember());
            } else if ("2".equals(choice)) {
                // 注册
                System.out.print("请设置顾客ID: ");
                customerId = stdIn.readLine();
                System.out.print("请输入姓名: ");
                String name = stdIn.readLine();
                System.out.print("请输入电话: ");
                String phone = stdIn.readLine();
                System.out.print("请设置密码: ");
                String password = stdIn.readLine();
                
                Customer customer = new Customer(customerId, name, phone, false, 0, password);
                if (customerDAO.registerCustomer(customer)) {
                    System.out.println("注册成功！");
                    // 通知老板增加会员数量
                    BossDAO bossDAO = new BossDAO();
                    bossDAO.updateMemberCount("B001", 1); // 假设老板ID为B001
                    showCustomerMenu(stdIn, customerId, false);
                } else {
                    System.out.println("注册失败");
                }
            } else {
                System.out.println("无效选择");
            }
            
        } catch (IOException e) {
            System.err.println("输入输出错误: " + e.getMessage());
        }
    }
    
    // 顾客菜单
    private static void showCustomerMenu(BufferedReader stdIn, String customerId, boolean isMember) throws IOException {
        CustomerDAO customerDAO = new CustomerDAO();
        ProductDAO productDAO = new ProductDAO();
        BossDAO bossDAO = new BossDAO();
        
        while (true) {
            System.out.println("\n顾客菜单:");
            System.out.println("1. 查询商品");
            System.out.println("2. 购买商品");
            if (!isMember) {
                System.out.println("3. 升级为会员");
            } else {
                System.out.println("3. 查看会员积分");
            }
            System.out.println("4. 退出");
            System.out.print("请选择: ");
            
            String choice = stdIn.readLine();
            
            switch (choice) {
                case "1":
                    // 查询商品
                    System.out.print("请输入商品ID: ");
                    String productId = stdIn.readLine();
                    Product product = productDAO.getProductById(productId);
                    if (product != null) {
                        System.out.println("商品ID: " + product.getProductId());
                        System.out.println("商品名称: " + product.getName());
                        System.out.println("售价: " + product.getPrice());
                        System.out.println("库存: " + product.getStock());
                    } else {
                        System.out.println("未找到该商品");
                    }
                    break;
                case "2":
                    // 购买商品
                    System.out.print("请输入商品ID: ");
                    productId = stdIn.readLine();
                    System.out.print("请输入购买数量: ");
                    int quantity;
                    try {
                        quantity = Integer.parseInt(stdIn.readLine());
                    } catch (NumberFormatException e) {
                        System.out.println("数量必须是数字");
                        break;
                    }
                    
                    product = productDAO.getProductById(productId);
                    if (product == null) {
                        System.out.println("未找到该商品");
                        break;
                    }
                    
                    if (product.getStock() < quantity) {
                        System.out.println("库存不足");
                        break;
                    }
                    
                    // 计算价格
                    double price = product.getPrice() * quantity;
                    double finalPrice = price;
                    
                    // 会员折扣
                    if (isMember) {
                        finalPrice = price * 0.9; // 9折
                        System.out.println("会员折扣后价格: " + finalPrice);
                    } else {
                        System.out.println("总价: " + finalPrice);
                    }
                    
                    // 支付方式
                    System.out.print("请选择支付方式(1.现金 2.扫码): ");
                    String payMethod = stdIn.readLine();
                    
                    if ("1".equals(payMethod)) {
                        System.out.print("请输入支付金额: ");
                        double payment;
                        try {
                            payment = Double.parseDouble(stdIn.readLine());
                        } catch (NumberFormatException e) {
                            System.out.println("金额必须是数字");
                            break;
                        }
                        
                        if (payment < finalPrice) {
                            System.out.println("支付金额不足");
                            break;
                        }
                        
                        System.out.println("找零: " + (payment - finalPrice));
                    }
                    
                    // 更新库存
                    productDAO.updateStock(productId, -quantity);
                    
                    // 更新老板净利润
                    double profit = (product.getPrice() - product.getPurchasePrice()) * quantity;
                    bossDAO.updateNetProfit("B001", profit);
                    
                    // 会员积分
                    if (isMember) {
                        customerDAO.updateMemberPoints(customerId, finalPrice / 10); // 消费10元得1积分
                        System.out.println("购买成功！已获得" + (finalPrice / 10) + "积分");
                    } else {
                        System.out.println("购买成功！");
                    }
                    break;
                case "3":
                    if (!isMember) {
                        // 升级为会员
                        if (customerDAO.upgradeToMember(customerId)) {
                            isMember = true;
                            System.out.println("升级为会员成功！");
                            // 通知老板更新会员数量
                            bossDAO.updateMemberCount("B001", 1);
                        } else {
                            System.out.println("升级失败");
                        }
                    } else {
                        // 查看会员积分
                        Customer customer = customerDAO.login(customerId, "");
                        if (customer != null) {
                            System.out.println("当前会员积分: " + customer.getMemberPoints());
                        }
                    }
                    break;
                case "4":
                    System.out.println("谢谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }
}
    